var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const pay = require('../../services/pay.js');
const App = getApp();
Page({
    data: {
        isMail:0,
        orderList: [],
        dd: '',
        hidden: false,
        page: 1,
        size: 10,
        hasMore: true,
        hasRefesh: false,
        status:0,
        textArr:['待付款','待配送','配送中','待提货','已提货','售后'],
        textMailArr:['待配送','待收货','已收货'],
        navH:0,
    },
    onLoad: function(options) {
        // 页面初始化 options为页面跳转所带来的参数
        if(options.status){
            this.setData({
                status:options.status
            })
        }
        this.getOrderList(this.data.status);
    },
    onPullDownRefresh: function () {
        this.getOrderList(this.data.status);
    },
    switchTab(e) {

        this.setData({
            isMail:e.currentTarget.dataset.flag
        })
        if(this.data.isMail==1){
            this.setData({
                status:2,
            })
        }else{
            this.setData({
                status:0,
            })
        }
        this.getOrderList(this.data.status);
    },
    gotoIndex: function(){
        wx.switchTab({
        url: '/pages/index/index',
        })
    },
    changeStatus:function(e){
        
        this.setData({
            status:e.currentTarget.dataset.status
        })
        this.getOrderList(this.data.status);

    },
    goback() {
        wx.navigateBack();
    },
    confirmTakeDelivery(e) {
        let _this = this;
        util.request(api.orderUrl.GrouponPickup+"?id="+e.currentTarget.dataset.id,{},'PUT').then(res=>{
            wx.showToast({
                title: '确认收货成功',
                icon:'none'
            })
            _this.getOrderList(_this.data.status);
        }).catch(err=>{
            console.log(err)
        })
    },
    pickUp(e){
        const _this = this;
        wx.showModal({
            title: '',
            content: '确认收货吗',
            success (res) {
                if (res.confirm) {
                    console.log('点击确定')
                    _this.orderPickUp(e);
                } else if (res.cancel) {
                    console.log('点击取消')
                }
            }
        });
    },
    orderPickUp(e){
        const id = e.currentTarget.dataset.orderId;
        wx.showLoading({
            title: "正在加载"
        })
        util.request(api.leader.orderPick,{
            id,
            self:true
        }, "POST")
            .then(res=>{
                wx.hideLoading();
                wx.showToast({
                    title: "确认成功",
                    icon: "none"
                })
                wx.navigateBack();
            }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    },
    getOrderList: function(status) {
        let that = this;
        let pram={
            pageNo:that.data.page,
            pageSize:that.data.size,
            status:''
        }
        if(status && status!=0){
            pram.status=status
        }
        let url = '';
        if(this.data.isMail==0){
            url = api.orderUrl.orderList
        }else if(this.data.isMail==1){
            url = api.orderUrl.GrouponOrderList
        }
        util.request(url,pram).then(function(res) {
            if (res.code == 200) {
                that.setData({
                    orderList: res.result
                });
                if (res.result.length < 10) {
                    that.setData({
                        hasMore: false,
                    })
                }

            }
        });
    },
    goIndex: function() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    onReady: function() {
        // 页面渲染完成
    },
    onShow: function() {
        // 页面显示
        this.setData({
            navH: App.globalData.navHeight
          });
    },
    onHide: function() {
        // 页面隐藏
    },
    onUnload: function() {
        // 页面关闭
    },
    loadMore: function(e) {
        var that = this;
        that.setData({
            hasRefesh: true,
        });
        if (!this.data.hasMore) return
        var url = api.orderUrl.orderList + "?pageNo=" + (++that.data.page) + '&pageSize=' + that.data.size;
        util.request(url).then(function(res) {
            if (res.code == 200) {
                that.setData({
                    orderList: that.data.orderList.concat(res.result),
                    hidden: true,
                    hasRefesh: false,
                });
                if (res.result.length < 10) {
                    that.setData({
                        hasMore: false,
                    })
                }
            }
        });
    },
    delOrder: function(event){
        let that = this;
        util.request(api.OrderDelete,{id:parseInt(event.currentTarget.dataset.orderId)}, 'POST').then(function(res) {
            if (res.errno === 0) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000
                });
                that.getOrderList();

            }
        });
    },
    payOrder: function(event) {
        pay.payOrder(parseInt(event.currentTarget.dataset.orderId),api.orderUrl.PayPrepayId).then(res => {
            wx.showModal({
                title: '温馨提示',
                content: '支付成功',
                success(res) {
                    wx.navigateTo({
                        url: '/pages/order/order'
                    })
                }
              })
        
        }).catch(res => {
            wx.showModal({
                title: '温馨提示',
                content: '支付失败',
                success(res) {
                    wx.navigateTo({
                        url: '/pages/order/order'
                    })
                }
              })
        });
    },

    gotoRefund: function(event) {
        wx.navigateTo({
            url: '/pages/refund/refund?id='+event.currentTarget.dataset.orderId
        })
    },
})